package Application.Widgets.FormattedTextFileds;

import Application.Utils.StaticUtil;
import Application.Widgets.Override.LabelOverride;

import javax.swing.*;
import java.text.NumberFormat;

public class TextFieldInteger extends JFormattedTextField
{
    private String WidgetName = "";

    public String getWidgetName() {
        return this.WidgetName;
    }
    public void setWidgetName(String name)
    {
        this.WidgetName = name;
    }
    public TextFieldInteger(int columns)
    {
        super(NumberFormat.getIntegerInstance());
        this.setColumns(columns);
        this.setFocusLostBehavior(JFormattedTextField.COMMIT);
        this.setFont(StaticUtil.config.fontGetter());
    }
    public TextFieldInteger(int columns,String Name,JComponent component)
    {
        super(NumberFormat.getIntegerInstance());
        this.setColumns(columns);
        this.setFocusLostBehavior(JFormattedTextField.COMMIT);
        this.setWidgetName(Name);
        this.setFont(StaticUtil.config.fontGetter());
        this.setColumns(100);
        component.add(this);
    }
    public TextFieldInteger(String labelText,int columns,String Name,JComponent component)
    {
        super(NumberFormat.getIntegerInstance());
        this.setColumns(columns);
        this.setFocusLostBehavior(JFormattedTextField.COMMIT);
        this.setWidgetName(Name);
        this.setFont(StaticUtil.config.fontGetter());
        LabelOverride label = new LabelOverride(labelText,component);
        component.add(this);
    }

    public int getInteger(int integer)
    {
        try {
            return Integer.parseInt(this.getText());
        }catch (Exception ex)
        {
            StaticUtil.errorReport.append(ex.getMessage()+"\n");
            StaticUtil.errorReport.append("不合法的输入，位置："+this.getWidgetName()+"\n");
            ex.printStackTrace();
            this.setText(String.valueOf(integer));
        }
            return integer;
    }

}